//
//  WmcEditBrandToolView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/9.
//

import UIKit
import YPBedrock

// MARK: - [WmcEditBrandToolView]

class WmcEditBrandToolView: WmcBaseView {
    lazy var titleLabel: UILabel = {
        let label = UILabel.YP.Create(text: "修改品牌图",
                                      font: 16.yp.uiFont(weight: .bold),
                                      color: UIColor.wmc_grayH2_323233,
                                      alignment: .center)
        return label
    }()

    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout
            .YP.CreateGridLayout(scrollDirection: .vertical,
                                 minimumLineSpacing: 20,
                                 minimumInteritemSpacing: 11,
                                 sectionInset: Wmc_standardEdge)
        return layout
    }()

    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView
            .YP.Create(layout: layout,
                       delegate: self,
                       dataSource: self,
                       registers: [
                           YPRegister(WmcEditBrandToolCell.self),
                       ])
        collectionView.isScrollEnabled = false
        return collectionView
    }()

    var toolList: [WmcEditBrandToolCell.ItemType] = [] {
        didSet { collectionView.reloadData() }
    }

    /// 点击事件
    var didSelectItem: YPBlock.OneParam<WmcEditBrandToolCell.ItemType>?

    override func makeUI() {

        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(44)
        }

        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
}

// MARK: - UICollectionViewDelegate

extension WmcEditBrandToolView: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let type = toolList.yp.object(at: indexPath.row) {
            didSelectItem?(type)
        }
    }
}

// MARK: - UICollectionViewDelegateFlowLayout

extension WmcEditBrandToolView: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lineCount: CGFloat = 4
        let lineSpacing = self.layout.minimumLineSpacing * (lineCount - 1)
        let horizontalEdge = self.layout.sectionInset.yp.horizontal
        let width = (collectionView.frame.size.width - lineSpacing - horizontalEdge) / lineCount - 1
        let height = width / 0.866666
        return CGSize(width: width, height: height)
    }
}

// MARK: - UICollectionViewDataSource

extension WmcEditBrandToolView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return toolList.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: WmcEditBrandToolCell = collectionView.yp.dequeueReusableCell(for: indexPath)
        if let type = toolList.yp.object(at: indexPath.row) {
            cell.configType(type)
        }
        return cell
    }
}
